package com.mti.epivisitemobile.view
{
	import com.mti.epivisitemobile.Constants;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import spark.components.View;
	
	public class ApplicationMediator extends Mediator
	{
		public static const NAME:String = "ApplicationMediator";
		
		public function ApplicationMediator(app:EpiVisiteMobile)
		{
			super(NAME, app);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				Constants.SHOW_ABOUT,
				Constants.SHOW_EVENT,
				Constants.SHOW_EVENT_LIST,
				Constants.SHOW_HOME,
				Constants.SHOW_HTML,
				Constants.SHOW_IMAGE,
				Constants.SHOW_MAP,
				Constants.SHOW_MEDIA_LIST,
				Constants.SHOW_QRCODE,
				Constants.SHOW_START,
				Constants.SHOW_VIDEO,
			];
		}
		
		// CALLED AT REGISTRATION - LISTEN FOR EVENTS, MEDIATE FIRST VIEW
		override public function onRegister():void
		{
			// Listen for Views to be added to the ViewNavigatorApplication
			app.addEventListener(Event.ADDED, handleAddedEvent);
			
			// Mediate the firstView (currently the StartView)
			sendNotification(Constants.MEDIATE_VIEW, app.getActiveView());
		}
		
		// HANDLE NOTIFICATIONS TO CHANGE VIEWS
		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case Constants.SHOW_ABOUT:
					app.showAboutView();
					break;
				
				case Constants.SHOW_EVENT:
					app.showEventView();
					break;
				
				case Constants.SHOW_EVENT_LIST:
					app.showEventListView();
					break;
				
				case Constants.SHOW_HOME:
					app.showHomeView();
					break;
				
				case Constants.SHOW_HTML:
					app.showHtmlView();
					break;
				
				case Constants.SHOW_IMAGE:
					app.showImageView();
					break;
				
				case Constants.SHOW_MAP:
					app.showMapView();
					break;
				
				case Constants.SHOW_MEDIA_LIST:
					app.showMediaListView();
					break;
				
				case Constants.SHOW_QRCODE:
					app.showQrView();
					break;
				
				case Constants.SHOW_START:
					app.showStartView();
					break;
				
				case Constants.SHOW_VIDEO:
					app.showVideoView();
					break;
			}
		}
		
		private function handleAddedEvent( event:Event ):void
		{
			if (event.target is View)
			{
				sendNotification(Constants.MEDIATE_VIEW, event.target);
			}
		}
		
		private function get app():EpiVisiteMobile
		{
			return viewComponent as EpiVisiteMobile;
		}
	}
}